﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LGSVR.Common;
using System.Data;

namespace LGSVR_WEB.sys
{
    public partial class MenuInfoEdit : DataBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "out", "<script>alert('系统超时或服务器故障，请退出重新登录！');window.parent.parent.navigate('../Logout.aspx');</script>");
            }
            if (!IsPostBack)
            {
                PageInit();
            }
        }

        private void PageInit()
        {
            Dictionary<string, string> dicSingle;

            // 编辑
            if (!string.IsNullOrEmpty(Request.QueryString["action"]) && Request.QueryString["action"].ToLower() == "edit")
            {
                btnSave.Text = "更 新";
                if (Utils.IsNumber(Request.QueryString["ID"]))
                {
                    dicSingle = GetSigle("menuInfo", "ID=" + Request.QueryString["ID"]);
                    if (dicSingle != null)
                    {
                        txtmenuID.Text = dicSingle["menuID"];
                        txtmenuName.Text = dicSingle["menuName"];
                        dropmodalName.SelectedItem.Text = dicSingle["modalName"];
                        txtmodalEName.Text = dicSingle["modalEName"];
                        txtactWeb.Text = dicSingle["actWeb"];
                        txtmenuEname.Text = dicSingle["menuEname"];
                    }
                    else
                    {
                        MsgContent = ShowMessage("数据加载失败。", MessageType.Error, true, false);
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> dicParam;

            dicParam = new Dictionary<string, string>();

            dicParam.Add("menuID", txtmenuID.Text.Trim());
            dicParam.Add("menuName", txtmenuName.Text.Trim());
            dicParam.Add("menuEname", txtmenuEname.Text.Trim());
            dicParam.Add("modalName", dropmodalName.SelectedItem.Text);
            dicParam.Add("modalEName",txtmenuEname.Text.Trim());
            dicParam.Add("actWeb", txtactWeb.Text.Trim());

            // 编辑
            if (!string.IsNullOrEmpty(Request.QueryString["action"]) && Request.QueryString["action"].ToLower() == "edit")
            {
                if (Utils.IsNumber(Request.QueryString["ID"]))
                {
                    Edit("menuInfo", dicParam, "ID=" + Request.QueryString["ID"]);

                    if (Result > 0)
                    {
                        MsgContent = ShowMessage("恭喜您，更新成功！", MessageType.Success, false, true);
                        NoRefresh();
                    }
                    else
                    {
                        if (Result == -100)
                        {
                            MsgContent = ShowMessage("对不起，操作失败，程序参数异常。", MessageType.Error, false, true);
                        }
                        else
                        {
                            MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
                        }
                    }
                }
            }
            else  // 新增
            {
                try
                {
                    Insert("menuInfo", dicParam);

                    if (Result > 0)
                    {
                        //得到菜单信息                   
                        DataTable dtMenu;
                        int totalRS;
                        dtMenu = GetList("userAct", " id desc ", "", 1, Int32.MaxValue, out totalRS);
                        foreach (DataRow dr in dtMenu.Rows)
                        {  
                            //往权限表里添加记录
                            Dictionary<string, string> dicParamuserAct;
                            dicParamuserAct = new Dictionary<string, string>();
                            dicParamuserAct.Add("ActID", dr["ActID"].ToString());
                            dicParamuserAct.Add("menuID", txtmenuID.Text.Trim());
                            string strstate = dr["ActName"].ToString().Trim().Contains("超级") ? "1" : "0";
                            dicParamuserAct.Add("state", strstate);
                            Insert("ActMenu", dicParamuserAct);
                        }


                        MsgContent = ShowMessage("恭喜您，添加成功！", MessageType.Success, false, true);
                        NoRefresh();
                    }
                }
                catch
                {                   
                    MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);                   

                }
                //Insert("menuInfo", dicParam);

                //if (Result > 0)
                //{
                //    MsgContent = ShowMessage("恭喜您，添加成功！", MessageType.Success, false, true);
                //    NoRefresh();
                //}
                //else
                //{
                //    MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
                //}
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["backUrlParam"]))
            {
                Response.Redirect("MenuInfoList.aspx?" + Request.QueryString["backUrlParam"], false);
            }
            else
            {
                Response.Redirect("MenuInfoList.aspx", false);
            }
        }
    }
}